﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Text;

namespace RabbitMQ_Common
{
    public class RabbitMQHelper
    {
        private static IConnection connection;
        private static IConnectionFactory connectionFactory;
        public static IConnection GetConnection()
        {
            if (connectionFactory == null)
            {
                connectionFactory = new ConnectionFactory()
                {
                    HostName = "82.157.163.36",
                    Port = 5672,
                    UserName = "admin",
                    Password = "Passwordqj123",
                    VirtualHost = "/test",
                    AutomaticRecoveryEnabled = false,
                };
            }
            if (connection==null||!connection.IsOpen)
            {
                connection = connectionFactory.CreateConnection();
                connection.ConnectionShutdown += Connection_ConnectionShutdown;
                //connection.ConnectionBlocked += Connection_ConnectionBlocked;
                //connection.ConnectionUnblocked += Connection_ConnectionUnblocked;
            }
            return connection;
        }

        private static void Connection_ConnectionShutdown(object sender, ShutdownEventArgs e)
        {
            Console.WriteLine("RabbitMQ已经断开连接");
        }

        private static void Connection_ConnectionBlocked(object sender, ConnectionBlockedEventArgs e)
        {
            Console.WriteLine("Connection_ConnectionBlocked");
        }

        private static void Connection_ConnectionUnblocked(object sender, EventArgs e)
        {
            Console.WriteLine("Connection_ConnectionUnblocked");
        }
    }


}
